package com.gmzg.gmzgoffice;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.test.mock.MockApplication;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

import java.lang.ref.WeakReference;

public class SignUpActivity extends AppCompatActivity {

    private EditText mEditTextUserName;
    private EditText mEditTextPwd;
    private EditText mEditTextPwdConfirm;
    // 定义一个变量，来标识是否退出
    private static boolean isExit = false;

    private static Handler mBackPressedHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };
    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    private Handler mHandler = new MyHandler(this);
    private static class MyHandler extends Handler {
        WeakReference<Activity> mActivityWeakReference;
        public MyHandler(SignUpActivity activity) {
            mActivityWeakReference=new WeakReference<Activity>(activity);
        }
        @Override
        public void handleMessage(Message msg) {
            final SignUpActivity activity = (SignUpActivity)mActivityWeakReference.get();
            int what = msg.what;
            switch (what){
                case 998:  // 注册成功
                    if (activity != null) {
                        if (activity != null) {
                            Toast.makeText(activity, "注册成功", Toast.LENGTH_SHORT).show();
                        }
                    }
                    //登陆
                    EMClient.getInstance().login(activity.mEditTextUserName.getText().toString().trim(), activity.mEditTextPwd.getText().toString().trim(), new EMCallBack() {
                        @Override
                        public void onSuccess() {
                            Intent intent = new Intent(activity, MainActivity.class);
                            ((GMZGApplication) activity.getApplication()).setUserName(activity.mEditTextUserName.getText().toString().trim());
                            SharedPreferences sp = activity.getSharedPreferences("userInfo", MODE_PRIVATE);
                            SharedPreferences.Editor edit = sp.edit();
                            edit.putString("user",activity.mEditTextUserName.getText().toString().trim());
                            edit.apply();
                            activity.startActivity(intent);
                            activity.finish();
                        }

                        @Override
                        public void onError(int i, String s) {
                            Message message = activity.mHandler.obtainMessage(102);
                            message.obj="登陆异常，请检查网络，从新登陆";
                            activity.mHandler.sendMessage(message);
                        }

                        @Override
                        public void onProgress(int i, String s) {

                        }
                    });
                    break;
                case 101://注册异常
                    Object obj = msg.obj;
                    if (obj != null) {
                        if (obj instanceof String) {
                            if (activity != null) {
                                Toast.makeText(activity, ((String) obj), Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                    break;
                case 102://注册后登陆异常
                    Object obj2 = msg.obj;
                    if (obj2 != null) {
                        if (obj2 instanceof String) {
                            if (activity != null) {
                                Toast.makeText(activity, ((String) obj2), Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                    Intent intent = new Intent(activity, LoginActivity.class);
                    activity.startActivity(intent);
                    activity.finish();
                    break;
            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        initView();
    }
    private void initView(){
        mEditTextUserName = ((EditText) findViewById(R.id.sign_up_user_name));
        mEditTextPwd = ((EditText) findViewById(R.id.sign_up_user_pwd));
        mEditTextPwdConfirm = ((EditText) findViewById(R.id.sign_up_confirm_user_pwd));
    }

    public void btn_sign_up(View view) {
        final String userName;
        final String userPwd;
        String userPwdConfirm;
        userName = mEditTextUserName.getText().toString().trim();
        userPwd = mEditTextPwd.getText().toString().trim();
        userPwdConfirm = mEditTextPwdConfirm.getText().toString().trim();

        if (userName.length() > 3 && userPwd.length() > 3 && userPwd.equals(userPwdConfirm)) {
            // TODO: 注册账号
            Thread thread = new Thread() {
                @Override
                public void run() {
                    // 同步代码
                    try {
                        // 只要方法不抛异常代表 注册成功
                        EMClient.getInstance().createAccount(userName, userPwd);
                        Message message = mHandler.obtainMessage(998);
                        mHandler.sendMessage(message);

                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        Message message = mHandler.obtainMessage(101);
                        message.obj="您注册的用户已经存在了，请换一个名字注册";
                        mHandler.sendMessage(message);
                    }
                }
            };
            thread.start();
        }else {
            Toast.makeText(this, "用户名和密码位数必须大于3位，两次输入密码必须一致", Toast.LENGTH_SHORT).show();
        }
    }


}
